import {
	baseIo
} from "@/config/url"
import {
	toast
} from '@/utils/lib'
class Wbs {
	constructor(status) {
	    this.status=status||'close' //当前状态
		this.socketTask=null
	}
	initConect() {
		this.socketTask=uni.connectSocket({
			url: baseIo,
			header: {
			  'content-type': 'application/json'
			},
			complete: ()=> {}
		})
		return new Promise((resolve, reject) => {
			//开启
			this.socketTask.onOpen(res => {
				console.log("连接成功")
				this.status='open'
				resolve(true)
			})
			// 失败回调
			this.socketTask.onError(res => {
				console.log("连接失败")
				this.status='close'
				resolve(false)
			})
			//关闭回调
			this.socketTask.onClose(res=> {
			  console.log('连接已关闭');
			  this.status='close'
			  // resolve(false)
			});
		})
	}
	closeConnect(){
		this.socketTask.close({
			success: res => {
				this.status='close'
			},
			fail: err => {
				console.log(err)
			}
		});
	}
	send(data) {
		return new Promise((resolve, reject) => {
			this.socketTask.send({
				data,
				success: res => {
					resolve(res)
				},
				fail: err => {
					console.log(err)
					reject(err)
				}
			});
		})
	}
}

export default new Wbs()
